package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 附件模块
 */
@Controller
@RequestMapping("/cargo/extCproduct")
@Slf4j
public class ExtCproductController extends BaseController {

    // 注入dubbo服务接口代理对象（注意导入的包）
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;

    /**
     * 附件列表
     */
    @RequestMapping("/list")
    public String list(String contractId,String contractProductId,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("执行查询购销合同的附件列表:");

        // 构造条件对象
        ExtCproductExample example = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = example.createCriteria();
        // 条件1：company_id
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        // 条件2：contract_product_id 根据货物查询其下的附件
        criteria.andContractProductIdEqualTo(contractProductId);
        // 根据登陆用户的企业id查询购销合同
        PageInfo<ExtCproduct> pageInfo =
                extCproductService.findByPage(example, pageNum, pageSize);

        // 查询附件的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        // 保存
        request.setAttribute("pageInfo", pageInfo);
        request.setAttribute("factoryList", factoryList);
        // 【保存附件需要下面2个参数：购销合同id、货物id】
        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);
        return "cargo/extc/extc-list";
    }


    /**
     * 添加或修改
     */
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct) {
        // 设置企业信息（从登陆用户中获取）
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());

        // 判断：根据id是否为空判断执行添加还是修改
        if (StringUtils.isEmpty(extCproduct.getId())) {
            // 添加
            extCproductService.save(extCproduct);
        } else {
            // 修改
            extCproductService.update(extCproduct);
        }

        return "redirect:/cargo/extCproduct/list?contractId="+extCproduct.getContractId()
                + "&contractProductId="+extCproduct.getContractProductId();
    }


    /**
     * 进入修改页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        // 根据货物id查询
        ExtCproduct extCproduct = extCproductService.findById(id);

        // 查询附件厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        // 保存 - 附件对象（contractProductId、contractId）
        request.setAttribute("extCproduct", extCproduct);
        request.setAttribute("factoryList", factoryList);
        return "cargo/extc/extc-update";
    }

    /**
     * 进入修改页面
     */
    @RequestMapping("/delete")
    public String delete(String id,String contractId,String contractProductId) {
        // 删除附件
        extCproductService.delete(id);
        // 重定向到列表
        return "redirect:/cargo/extCproduct/list?contractId="+contractId
                + "&contractProductId="+contractProductId;
    }
}